package chapter11;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-23 下午12:43
 */
public class SerializeTest {
    public static void main(String[] args) {
        Book book = new Book("test", 12.343);

        File file = new File(File.separator + "home" + File.separator + "lzy" + File.separator + "logs" + File.separator + "test" + File.separator + "test.dat");

        try (ByteArrayOutputStream mem = new ByteArrayOutputStream();
             ObjectOutputStream stream = new ObjectOutputStream(mem)) {
            stream.writeObject(book);
            byte[] dat = mem.toByteArray();
            System.out.println("serialized object:" + new String(dat));
            try (ByteArrayInputStream in = new ByteArrayInputStream(dat);
                 ObjectInputStream stream1 = new ObjectInputStream(in)) {
                Object obj = stream1.readObject();
                System.out.println("object type is " + obj.getClass().getName());
                Book b = (Book) obj;
                System.out.println("after deserialized:" + b.toString());
                System.out.println("book1 is same with book2?:" + String.valueOf(book == b));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
